package com.herench.bs.handler;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicAppointmentRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.utils.DataFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * com.herench.bs.handler
 * Created by wangwenkai on 2017/9/22/14:49
 * <p>
 * 查询是否可支付（预约报到）
 */
@Component
@Handler(code = "100725" ,name="查询是否可支付")
public class CheckAppointmentStateHandler extends AbstractMessageHandler {


    @Autowired
    private IClinicAppointmentRecordDao clinicAppointmentRecordDao;


    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String id = requestMessage.getArgsPropertyStringValue("recordId");
        String source = requestMessage.getArgsPropertyStringValue("source");
        String user = requestMessage.getArgsPropertyStringValue("user");
        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        Map<String, Object> sendParam = new HashMap<>(16);
        sendParam.put("id", id);
        Map<String, Object> resDao = clinicAppointmentRecordDao.findOne(sendParam);
        String idCard = (String) resDao.get("idCard");
        String hisClinicAppointsId = (String) resDao.get("hisClinicAppointsId");
        String appointId = (String) resDao.get("appointId");
        Date appointDateRes = (Date) resDao.get("appointDate");
        String period = (String) resDao.get("period");
        String[] arr = period.split("~");
        int starTime = Integer.parseInt(arr[0].replace(":", ""));
        int endTime = Integer.parseInt(arr[1].replace(":", ""));
        String appointDateStr = DataFormatUtils.getSimpleTimeString(appointDateRes);
        Date appointDate = DataFormatUtils.dateTimeToDate(appointDateRes);
        Map<String, Object> res = new HashMap<>(16);
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        Map<String,Object> resUnpaidOrders = outpatientClinicAgent.loadUnpaidOrders(channelCode,hospitalCode,idCard,source,user,"3");
        if (Dialect.getSuccessfulState().equals(resUnpaidOrders.get("code"))) {
            res.put("dataList",resUnpaidOrders.get("dataList"));
            res.put(ResponseMessage.RES_CODE, "-001");
            res.put(ResponseMessage.MESSAGE, "存在未支付订单");
            res.put(ResponseMessage.MESSAGE_IN, "存在未支付订单");
            res.put(ResponseMessage.MESSAGE_OUT, "存在未支付订单");
        }else{
        Date date = new Date();
        int dateHourMinute = Integer.parseInt(DataFormatUtils.getHourMinuteString(date).replace(":", ""));
        Date dateFormat = DataFormatUtils.dateTimeToDate(date);
        //比较当前时间与预约就诊时间
        int resCompareDate = dateFormat.compareTo(appointDate);
        Boolean compareTime = dateHourMinute >= starTime && dateHourMinute <= endTime;
        if (resCompareDate == 0 && compareTime) {
            IOutpatientClinicAgent iOutpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
            Map<String, Object> resPatient = iOutpatientClinicAgent.extractCreationPatient(channelCode, hospitalCode, idCard);
            String patientId = (String) resPatient.get("patientId");
            if (!(patientId == null || "".equals(patientId))) {
                res = iOutpatientClinicAgent.checkAppointmentState(channelCode, hospitalCode, appointId,hisClinicAppointsId, patientId, idCard,id);
            } else {
                res = resPatient;
            }
        } else {
            if (resCompareDate < 0) {
                res.put(ResponseMessage.RES_CODE, "-19");
                res.put(ResponseMessage.MESSAGE_IN, "支付时间未到！请在" + appointDateStr + " " + period + "进行支付");
                res.put(ResponseMessage.MESSAGE, "支付时间未到！请在" + appointDateStr + " " + period + "进行支付");
                res.put(ResponseMessage.MESSAGE_OUT, "支付时间未到！请在" + appointDateStr + " " + period + "进行支付");
            }
            if (resCompareDate > 0) {
                res.put(ResponseMessage.RES_CODE, "-19");
                res.put(ResponseMessage.MESSAGE_IN, "此预约号支付时间已过，请您到窗口报到");
                res.put(ResponseMessage.MESSAGE, "此预约号支付时间已过，请您到窗口报到");
                res.put(ResponseMessage.MESSAGE_OUT, "此预约号支付时间已过，请您到窗口报到");
            }
            if (resCompareDate == 0) {
                if (dateHourMinute < starTime) {
                    res.put(ResponseMessage.RES_CODE, "-19");
                    res.put(ResponseMessage.MESSAGE_IN, "支付时间未到！请在" + appointDateStr + " " + period + "进行支付");
                    res.put(ResponseMessage.MESSAGE, "支付时间未到！请在" + appointDateStr + " " + period + "进行支付");
                    res.put(ResponseMessage.MESSAGE_OUT, "支付时间未到！请在" + appointDateStr + " " + period + "进行支付");
                }
                if (dateHourMinute > endTime) {
                    res.put(ResponseMessage.RES_CODE, "-19");
                    res.put(ResponseMessage.MESSAGE_IN, "此预约号支付时间已过，请您到窗口报到!");
                    res.put(ResponseMessage.MESSAGE, "此预约号支付时间已过，请您到窗口报到");
                    res.put(ResponseMessage.MESSAGE_OUT, "此预约号支付时间已过，请您到窗口报到");
                }
            }
        }
//        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
//        Map<String,Object> resUnpaidOrders = outpatientClinicAgent.loadUnpaidOrders(channelCode,hospitalCode,idCard,source,user,"3");
//        if (Dialect.getSuccessfulState().equals(resUnpaidOrders.get("code"))) {
//            res.put("dataList",resUnpaidOrders.get("dataList"));
//            res.put(ResponseMessage.RES_CODE, "-001");
//            res.put(ResponseMessage.MESSAGE, "存在未支付订单");
//            res.put(ResponseMessage.MESSAGE_IN, "存在未支付订单");
//            res.put(ResponseMessage.MESSAGE_OUT, "存在未支付订单");
//        }
    }
        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
